package com.confidena

import com.confidena.security.Person

class PersonGame {
	static belongsTo = [person:Person]
	Game game
	Boolean taskPay
	Date taskPayDate
	//Boolean taskInvite
	//Date taskInviteDate
	Integer creditBalance
	Date creationDate
	Integer state
	Boolean isBoot
	GameList gameList
    GameList gameListOut
	GameCircle gameCircleOrigin
	
	static hasMany = [gameCircle:GameCircle, lists:GameList]
	
	static mappedBy = [gameCircleOrigin:"personGameCreated", gameCircle:"personGame"]

    static constraints = {
		taskPayDate nullable:true
		//taskInviteDate nullable:true
		gameList nullable:true
		gameListOut nullable:true
    }
	
	static mapping = {
		
	}
	
	String toString() {
		"${name}-${person.username}"
	}
	
	//consultas
	def getFirstGameList(){
		if(!this.gameList?.personGames?.size == 0){
			return this.gameList.personGames[0]
		}
	}
	
	def getName(){
		return this.game.name.concat(" I1")
	}
	
	def getTaskInvited(){
		if(this.gameCircle?.findAll{w -> w.state.equals(CircleState.ACTIVO)}.size == this.game?.circleSize){
			return true
		}
		return false
	}
	
	def getDateTaskInvited(){
		if(this.taskInvited){
			return this.gameCircle.sort{it.dateEvent}[0]
		}
		return false
	}
	
	def getRanking(){
		def result = DateUtil.getSecondsDifferenceInDates(this.creationDate, this.taskPayDate)
		def points
		def secondsExpirationTime = this.game.expirationTime * 24 * 60 * 60
				
		if(result > secondsExpirationTime){
			//no tiene puntos
		} else {
			if (result > (secondsExpirationTime * 0.75)){
				points = 1
			} else {
				if (result > (secondsExpirationTime * 0.5)){
					points = 2
				} else {
					if(result > (secondsExpirationTime * 0.25)){
						points = 4
					}else{
						points = 6
					}
				}
			}
		}
		
		return points
	}
}




